import { createRouter, createWebHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import { useUserStore } from '../store/modules/user'

// 定义路由
const routes: RouteRecordRaw[] = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/login/index.vue'),
    meta: {
      title: '登录',
      hidden: true,
      requiresAuth: false
    }
  },
  {
    path: '/',
    name: 'Layout',
    component: () => import('@/layouts/index.vue'),
    meta: {
      title: '布局',
      requiresAuth: true
    },
    children: [
      {
        path: '',
        redirect: 'dashboard'
      },
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index.vue'),
        meta: {
          title: '仪表盘',
          icon: 'pi pi-home',
          requiresAuth: true
        }
      },
      // 系统管理相关路由
      {
        path: 'system/user/list',
        name: 'UserList',
        component: () => import('@/views/system/user/index.vue'),
        meta: {
          title: '用户管理',
          icon: 'pi pi-users',
          requiresAuth: true
        }
      },
      {
        path: 'system/dept/list',
        name: 'DeptList',
        component: () => import('@/views/system/dept/list.vue'),
        meta: {
          title: '部门管理',
          icon: 'pi pi-building',
          requiresAuth: true
        }
      },
      {
        path: 'system/role/list',
        name: 'RoleList',
        component: () => import('@/views/system/role/list.vue'),
        meta: {
          title: '角色管理',
          icon: 'pi pi-key',
          requiresAuth: true
        }
      },
      {
        path: 'system/post/list',
        name: 'PostList',
        component: () => import('@/views/system/post/list.vue'),
        meta: {
          title: '岗位管理',
          icon: 'pi pi-briefcase',
          requiresAuth: true
        }
      },
      {
        path: 'system/menu/list',
        name: 'MenuList',
        component: () => import('@/views/system/menu/list.vue'),
        meta: {
          title: '菜单管理',
          icon: 'pi pi-list',
          requiresAuth: true
        }
      },
      {
        path: 'system/log/list',
        name: 'LogList',
        component: () => import('@/views/system/log/list.vue'),
        meta: {
          title: '日志管理',
          icon: 'pi pi-file',
          requiresAuth: true
        }
      },
      {
        path: 'system/org/list',
        name: 'OrgList',
        component: () => import('@/views/system/org/list.vue'),
        meta: {
          title: '组织架构',
          icon: 'pi pi-sitemap',
          requiresAuth: true
        }
      },
      // 流程管理相关路由
      {
        path: 'workflow/category/list',
        name: 'WorkflowCategory',
        component: () => import('@/views/workflow/category/index.vue'),
        meta: {
          title: '流程分类',
          icon: 'pi pi-tags',
          requiresAuth: true
        }
      },
      {
        path: 'workflow/definition/list',
        name: 'WorkflowDefinition',
        component: () => import('@/views/workflow/definition/index.vue'),
        meta: {
          title: '流程定义',
          icon: 'pi pi-file-edit',
          requiresAuth: true
        }
      },
      {
        path: 'workflow/instance/list',
        name: 'WorkflowInstance',
        component: () => import('@/views/workflow/instance/index.vue'),
        meta: {
          title: '流程实例',
          icon: 'pi pi-clone',
          requiresAuth: true
        }
      },
      {
        path: 'workflow/history/list',
        name: 'WorkflowHistory',
        component: () => import('@/views/workflow/history/index.vue'),
        meta: {
          title: '历史流程',
          icon: 'pi pi-history',
          requiresAuth: true
        }
      },
      // 任务管理相关路由
      {
        path: 'task/todo/list',
        name: 'TaskTodo',
        component: () => import('@/views/task/todo/index.vue'),
        meta: {
          title: '待办任务',
          icon: 'pi pi-inbox',
          requiresAuth: true
        }
      },
      {
        path: 'task/done/list',
        name: 'TaskDone',
        component: () => import('@/views/task/done/index.vue'),
        meta: {
          title: '已办任务',
          icon: 'pi pi-check-circle',
          requiresAuth: true
        }
      },
      // 路由测试页面
      {
        path: 'route-test',
        name: 'RouteTest',
        component: () => import('@/views/RouteTest.vue'),
        meta: {
          title: '路由测试',
          icon: 'pi pi-test-tube',
          requiresAuth: true
        }
      },
      
      // 原有设置相关路由
      {
        path: 'settings',
        name: 'Settings',
        component: () => import('@/views/settings/index.vue'),
        meta: {
          title: '系统设置',
          icon: 'pi pi-cog',
          requiresAuth: true
        }
      },
      {
        path: 'profile',
        name: 'Profile',
        component: () => import('@/views/settings/index.vue'),
        meta: {
          title: '个人中心',
          icon: 'pi pi-user',
          requiresAuth: true
        },
        props: {
          activeTab: 'profile'
        }
      }
    ]
  },
  {
    path: '/:pathMatch(.*)*',
    name: '404',
    component: () => import('@/views/error-page/404.vue'),
    meta: {
      title: '页面不存在',
      hidden: true,
      requiresAuth: false
    }
  },
  {
    path: '/403',
    name: '403',
    component: () => import('@/views/error-page/403.vue'),
    meta: {
      title: '没有权限',
      hidden: true,
      requiresAuth: false
    }
  }
]

// 创建路由实例
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
})

// 路由守卫
router.beforeEach((to, _from, next) => {
  const userStore = useUserStore()
  const isAuthenticated = userStore.isAuthenticated

  // 设置页面标题
  document.title = `${to.meta.title || 'EverWin Admin'} - ${import.meta.env.VITE_APP_NAME || 'Admin Panel'}`

  // 检查是否需要认证
  if (to.meta.requiresAuth && !isAuthenticated) {
    next('/login')
  } else {
    // 其他情况允许通过
    next()
  }
})

export default router